package com.example.mq.mqserver.datacenter;

import com.example.mq.MqApplication;
import com.example.mq.mqserver.core.Binding;
import com.example.mq.mqserver.core.Exchange;
import com.example.mq.mqserver.core.ExchangeType;
import com.example.mq.mqserver.core.MSGQueue;
import com.example.mq.mqserver.mapper.MetaMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;

@Slf4j
public class DataBaseManager {

    private MetaMapper metaMapper;

    public void init() {
        //手动获取到metaMapper
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        //建库建表 插入一些默认数据
        //如果数据库已经存在 不做任何操作 , 如果数据库不存在 则创建库创建表 构造默认数据
        //根据meta.db文件是否存在来做判断
        if (!checkDBExists()) {
            //数据库不存在 就进行建库建表操作
            //先创建一个data目录
            File dataDir = new File("./data");
            dataDir.mkdirs();
            //不存在 创建表
            createTable();
            createDefaultData();
            log.info("[DataBaseManager] 数据库初始化完成");
        } else {
            //数据库已经存在
            log.info("[DataBaseManager] 数据库已经存在");
        }
    }

    private boolean checkDBExists() {
        //判断meta.db文件是否存在
        File file = new File("./data/meta.db");
        return file.exists();
    }

    //建表操作 不需要建库
    //首次执行数据库造作 就会自动创建meta.db文件(Mybatis执行)
    private void createTable() {
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        log.info("[DataBaseManager] 创建表完成");
    }

    //创建默认数据
    //主要是添加默认交换机
    private void createDefaultData() {
        //构造默认交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        log.info("[DataBaseManager] 创建默认数据完成");
    }

    //封装其他数据库的操作
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public void insertQueue(MSGQueue queue) {
        metaMapper.insertQueue(queue);
    }

    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public List<Exchange> selectAllExchanges() {
        return metaMapper.selectAllExchanges();
    }
    public List<MSGQueue> selectAllQueues(){
        return metaMapper.selectAllQueues();
    }

    public List<Binding> selectAllBindings(){
        return metaMapper.selectAllBindings();
    }

    public void deleteDB(){
        File file = new File("./data/meta.db");
        boolean ret = file.delete();
        if(ret){
            log.info("[DataBaseManager] 删除数据库文件成功");
        }else {
            log.info("[DataBaseManager] 删除数据库文件失败");
        }

        File dataDir = new File("./data");
        //使用delete删除目录要保证目录是空的
        ret = dataDir.delete();
        if(ret){
            log.info("[DataBaseManager] 删除数据库目录成功");
        }else {
            log.info("[DataBaseManager] 删除数据库目录失败");
        }
    }

}
